package fr.web325.servlet.client.service;

import fr.web325.bean.User;
import fr.web325.dao.ClientDao;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

@WebServlet("/shop/client/uploadUser")
public class UploadUser extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = new User();
        HttpSession session = req.getSession();
        int id = ((User)session.getAttribute("user")).getId();
        try {
            BeanUtils.populate( user,req.getParameterMap());
            user.setId(id);
            if(new ClientDao().uploadUser(user)){
                // 修改成功
                session.setAttribute("user",new ClientDao().getUserById(id));
                req.getRequestDispatcher("mine").forward(req,resp);
            }
        } catch (IllegalAccessException | InvocationTargetException | SQLException e) {
            e.printStackTrace();
        }
    }
}
